package com.kk.goGame.utils;
/*
 *@author KK on 2023.02.17 17:00
 */

import com.kk.goGame.entity.Email;
import com.qf.framwork.utils.PropertiesUtils;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailUtils {
    public static int sendEmail(Email email){
        ThreadPoolUtils.submit(() -> {
            //1、创建Session对象
            Session session = Session.getDefaultInstance(PropertiesUtils.getPropertiesObj());
            //2、创建一封邮件
            MimeMessage mimeMessage = new MimeMessage(session);

            try {
                //3、给邮件设置相关的属性
                //设置标题
                mimeMessage.setSubject(email.getSubject(), "UTF-8");
                //发件人
                mimeMessage.setFrom(new InternetAddress(PropertiesUtils.getProperties("mail.user")));
                //收件人
                //接收者的类型
                // - Message.RecipientType.TO 表示普通接收者
                // - Message.RecipientType.CC 表示抄送者
                // - Message.RecipientType.BCC 表示密送者
                mimeMessage.setRecipients(Message.RecipientType.TO, email.getTo());
                //内容
                mimeMessage.setContent(email.getContent(), "text/html;charset=utf-8");

                //4、通过session发送邮件
                Transport transport =session.getTransport();
                //5、链接SMTP服务器 通过账号密码
                transport.connect(PropertiesUtils.getProperties("mail.user"),
                        PropertiesUtils.getProperties("mail.password"));
                transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
                System.out.println("邮件发送完成！");
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return 1;
    }
}
